﻿using System;
using TG.Timer;
using UnityEngine;

namespace TG
{

    [Serializable]
    public class MapCell
    {
        /// <summary>
        /// 使用次数防止共用格子
        /// </summary>
        public int useNum;
        /// <summary>
        /// 地图组物品
        /// </summary>
        public MapItemType itemType;

        /// <summary>
        /// 地图块唯一标识
        /// </summary>
        public int mapItemId;

        /// <summary>
        /// 格子位置
        /// </summary>
        public Vector3 position;

        #region 测试使用数据

        public bool is9;
        
        public bool is25;

        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="canUse"></param>
        /// <param name="isUse"></param>
        /// <param name="itemType"></param>
        public MapCell(int mapItemId, Vector3 position)
        {
            this.mapItemId = mapItemId;
            this.position = position;
            TimerManager.Instance.AddFrameElapsed(DrawCell);
        }


        void DrawCell()
        {
            float mapCellHalf = Default.MapCellWidth / 2f;

            Vector3 bottomLeft = new Vector3(position.x - mapCellHalf, 0, position.z - mapCellHalf);
            Vector3 bottomRight = new Vector3(position.x + mapCellHalf, 0, position.z - mapCellHalf);
            Vector3 topLeft = new Vector3(position.x - mapCellHalf, 0, position.z + mapCellHalf);
            Vector3 topRight = new Vector3(position.x + mapCellHalf, 0, position.z + mapCellHalf);

            Color color = Color.white;
            if (is9)
                color = Color.green;
            else if (is25)
                color = Color.red;
            if (itemType != MapItemType.None)
                color = Color.black;

            Debug.DrawLine(bottomLeft, bottomRight, color, Time.deltaTime);
            Debug.DrawLine(bottomLeft, topLeft, color, Time.deltaTime);
            Debug.DrawLine(bottomRight, topRight, color, Time.deltaTime);
            Debug.DrawLine(topLeft, topRight, color, Time.deltaTime);
        }

        public void SetData(MapItemType itemType)
        {
            this.itemType = itemType;
        }

        public void ResetMapCellType()
        {
            itemType = MapItemType.None;
            useNum--;
            if (useNum <= 0)
            {
                is25 = false;
                is9 = false;
            }
        }

        public MapItemType GetMapCellType()
        {
            return itemType;
        }

        public void Clear()
        {
            itemType = MapItemType.None;
            useNum = 0;
            is25 = false;
            is9 = false;
        }
    }
}
